package com.firewolf.springcloud.controller;

import com.firewolf.springcloud.pojo.User;
import com.firewolf.springcloud.vo.ResponseVO;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 作者：刘兴
 * 时间:19/2/12
 **/
@RestController
@RequestMapping("/user")
@Api(description = "用户接口")
public class UserController {

    @Value("${server.port}")
    private String serverPort;

    @Autowired
    private DiscoveryClient client;

    Logger logger = LoggerFactory.getLogger(getClass());

    @GetMapping
    public User findUser() {
        logger.error("调用到了provider");

        return new User(1, serverPort);
    }

    @GetMapping("/servers")
    public String servers() {
        List<String> services = client.getServices();
        for (String s : services) {
            System.out.println(s);
            List<ServiceInstance> instances = client.getInstances(s);
            for (ServiceInstance ss : instances) {
                System.out.println(ss.getHost() + "," + ss.getPort());
            }
        }
        return "ok";
    }


    //Rest请求
    @PostMapping
    @ApiOperation(value = "添加用户", httpMethod = "POST")
    @ApiImplicitParams(@ApiImplicitParam(name = "u", value = "这是用户信息"))
    @ApiResponses(
            value = {
                    @ApiResponse(code = 200, message = "成功"),
                    @ApiResponse(code = 500, message = "内部错误")
            }
    )
    public ResponseVO add(@RequestBody User u) {
        try {
            //调用service添加用户
            System.out.println(u);
            return ResponseVO.ok("添加成功");
        } catch (Exception e) {
            return ResponseVO.error("添加失败");
        }
    }


    @PostMapping("/a")
    public ResponseVO add(Integer id, String name) {
        try {
            //调用service添加用户
            System.out.println(id + "," + name);
            return ResponseVO.ok("添加成功");
        } catch (Exception e) {
            return ResponseVO.error("添加失败");
        }
    }


    @GetMapping("/b")
    public List<User> get() {
        return Arrays.asList(new User(6, "张三"), new User(7, "李四"));
    }

    @PostMapping("/c")
    public List<User> get(@RequestBody  User u) {
        System.out.println(u);
        return Arrays.asList(new User(6, "张三"), new User(7, "李四"));
    }

    //Rest风格，  对任意资源如user的增删改查使用请求方式POST，DELETE,PUT,GET来表示，

    //user

    @GetMapping("/d")
    public List<User> get(Integer id,String name) {
        System.out.println(id+","+name);
        return Arrays.asList(new User(6, "张三"), new User(7, "李四"));
    }

    //占位符传参
    @DeleteMapping("/e/{id}/xxx/{name}")
    public ResponseVO delete(@PathVariable("id") Integer uid,@PathVariable("name") String uname){
        System.out.println(uid+","+uname);
        return ResponseVO.ok("删除成功");
    }

}

